import Vue from 'vue'
import VueRouter from 'vue-router'
import Denglu from '../views/denglu/denglu'
import Home from '../views/home/home'
import Product from '../views/home/pm/product/index'
import AddProduct from '../views/home/pm/addProduct/index'
import ProductCategory from '../views/home/pm/productCategory/index'
import AddProductCategory from '../views/home/pm/productCategory/add'
import UpdataProductCategory from '../views/home/pm/productCategory/updata'
import Order from '../views/home/om/order/index'
import Setting from '../views/home/om/setting/index'

import Admin from '../views/home/um/admin/index'
import Role from '../views/home/um/role/index'
import Menu from '../views/home/um/menu/index'
import AllocMenu from "../views/home/um/role/allocMenu"





Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: "/denglu"
    },
    {
        path: '/home',
        name: 'home',
        component: Home,

    },
    //pm
    {
        path: '/pm',
        name: 'pm',
        component: Home,
        children: [
            {
                path: '/Pm/',
                redirect: "/pm/product"
            },
            {
                path: '/pm/product',
                name: 'product',
                component: Product
            },
            {
                path: '/pm/addProduct',
                name: 'addProduct',
                component: AddProduct
            },
            {
                path: '/pm/productCategory',
                name: 'productCategory',
                component: ProductCategory
            },
            {
                path: '/pm/productCateAdd',
                name: 'productCateAdd',
                component: AddProductCategory
            },
            {
                path: '/pm/productCateUpdate',
                name: 'productCateUpdate',
                component: UpdataProductCategory
            },
        ]
    },
    //om
    {
        path: '/om',
        name: 'om',
        component: Home,
        children: [
            {
                path: '/om/',
                redirect: "/om/order"
            },
            {
                path: '/om/order',
                name: 'order',
                component: Order
            },
            {
                path: '/om/setting',
                name: 'setting',
                component: Setting
            },
           
        ]
    },
    //um
    {
        path: '/um',
        name: 'um',
        component: Home,
        children: [
            {
                path: '/um/',
                redirect: "/um/admin"
            },
            {
                path: '/um/admin',
                name: 'admin',
                component: Admin
            },
            {
                path: '/um/role',
                name: 'role',
                component: Role
            },
            {
                path: '/um/allocMenu',
                name: 'allocMenu',
                component: AllocMenu
            },
            {
                path: '/um/menu',
                name: 'menu',
                component: Menu
            },
        ]
    },
    {
        path: '/denglu',
        name: 'denglu',
        component: Denglu
    },


]

const router = new VueRouter({
    routes
})

// 全局守卫
router.beforeEach((to,from,next)=>{
    let token = window.sessionStorage.getItem("token");
    if(to.path === "/denglu"){
      // 在这里判断是否登录
      if(token){
        // 已登录
        return next("/")
      }else{
        // 没有登录
        return next();
      }
    }
    if(!token){
      return next("/denglu")
    }
    next();
  })


const RouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return RouterPush.call(this, to).catch(err => err)
}
const RouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(to) {
    return RouterReplace.call(this, to).catch(err => err)
}

export default router